<template>
  <cml-module-item :title="$t('cmlpanel.prompts')"
    :titleMore="$t('cmlpanel.prompts')"
    v-bind="$attrs"
    v-on="$listeners">
    <a-switch size="small"
      :disabled="!isReady"
      :checked="isStart"
      @change="aiVoiceFeedbackChange" />
    <template v-slot:detail>
      <voice-feedback-more ref="voiceFeedbackMore"></voice-feedback-more>
    </template>
  </cml-module-item>
</template>
<script>
import { Switch } from 'ant-design-vue'
import CmlModuleItem from '../../cml-module-item'
import VoiceFeedbackMore from './morePanel'
import { voiceFeedbackServer as vfs } from 'anov-core'

export default {
  name: 'VoiceFeedback',
  inheritAttrs: false,
  components: {
    ASwitch: Switch,
    CmlModuleItem,
    VoiceFeedbackMore
  },
  model: {},
  props: {},
  data() {
    return {
      isReady: false
    }
  },
  computed: {
    ...vfs.mapGetters(['isStart', 'synthesisType']),
    titleText() {
      return this.$t('cmlpanel.prompts')
    },
    titleMore() {
      return this.$t('cmlpanel.prompts')
    }
  },
  watch: {},
  created() {
    this.isReady = vfs.isReady
  },
  mounted() {
    app.envServer.addEventListener('onLine', () => {
      this.testVoiceServer()
    })
    app.envServer.addEventListener('offLine', () => {
      this.testVoiceServer()
    })
    
  },
  beforeDestroy() {},
  methods: {
    aiVoiceFeedbackChange(e) {
      vfs.toggleOpen(e)
      let msg = this.titleText + (this.isStart ? this.$t('cmlpanel.start') : this.$t('cmlpanel.stop'))
      vfs.speak(msg)
      app.msg({type: 'info', content: msg})

      // this.testVoiceServer()
    },
    testVoiceServer(){
      if(this.synthesisType == 'xf'){
        if(!app.envServer.localNetwork.connected){
          setTimeout(() => {
            let msg = '网络连接断开，语音提示接口已切换到默认'
            app.msg({type: 'error', content: msg})
            vfs.changeSynthesisType('default')
            vfs.speak(msg)
          }, 100)
        }else {
          setTimeout(() => {
            let defaultVolume = vfs.getOption().volume
            vfs.test("测试", {volume: 0})
            vfs.addEventListener('testStart', () => {
              vfs.setOption({volume: defaultVolume})
            })
            vfs.addEventListener('testError', err => {
              let msg = '语音提示接口智灵不可用，已为您切换为默认'
              app.msg({type: 'error', content: msg})
              vfs.changeSynthesisType('default')
              vfs.speak(msg,{volume:defaultVolume})
            })
          }, 1000)
        }
        
        
      }
    }
  }
}
</script>
<style lang="scss" scoped>
</style>